package com.party.admin.biz.distribution;

import com.party.core.model.distributor.DistributorCount;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.distributor.IDistributorDetailService;
import com.party.core.service.distributor.IDistributorRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分销业务逻辑业务接口
 * Created by wei.li.
 *
 * @date 2018/2/5 0005
 * @time 18:38
 */
@Service
public class DistributorCountBizService {

    @Autowired
    private IDistributorRelationService distributorRelationService;

    @Autowired
    private IDistributorDetailService distributorDetailService;

    @Autowired
    private IDistributorCountService distributorCountService;

    public void init(){
        //查询所有的 distributor_relation type 为4
        DistributorRelation distributor = new DistributorRelation();
        distributor.setType(4);
        List<DistributorRelation> list = distributorRelationService.list(distributor);
        for (DistributorRelation relation : list){

            //根据 distributor_detail 查询众筹成功的条数
            Integer num = distributorDetailService.countSccessCrowdfund(relation.getId());

            //初始化到 distributor_count
            DistributorCount distributorCount = distributorCountService.findByRelationId(relation.getId());
            if (null == distributorCount){
                distributorCount = new DistributorCount(relation.getId());
                distributorCountService.insert(distributorCount);
            }
            distributorCount.setSuccessNum(num);
            distributorCountService.update(distributorCount);
        }
    }
}
